package com.shioirikukuri.mapper;

import com.shioirikukuri.entity.Food;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;

@Mapper // 标记为MyBatis Mapper接口
public interface FoodMapper {

	/**
	 * 查询所有菜品信息
	 *
	 * @return 菜品列表
	 */
	@Select("SELECT * FROM foods")
	List<Food> selectAllFoods();

	/**
	 * 根据ID查询菜品信息
	 *
	 * @param id 菜品ID
	 * @return 匹配的菜品对象
	 */
	@Select("SELECT * FROM foods WHERE id = #{id}")
	Food selectFoodById(@Param("id") Long id);

	/**
	 * 插入新的菜品信息
	 *
	 * @param food 新菜品的信息
	 */
	@Insert("INSERT INTO foods(name, description, price, imageUrl, available, categoryId, createdAt, updatedAt) " +
			"VALUES(#{name}, #{description}, #{price}, #{imageUrl}, #{available}, #{categoryId}, NOW(), NOW())")
	@Options(useGeneratedKeys = true, keyProperty = "id") // 自动获取插入记录的主键值
	void insertFood(Food food);

	/**
	 * 更新已有菜品的信息
	 *
	 * @param food 需要更新的菜品信息
	 */
	@Update("UPDATE foods SET name=#{name}, description=#{description}, price=#{price}, imageUrl=#{imageUrl}, " +
			"available=#{available}, categoryId=#{categoryId}, updatedAt=NOW() WHERE id=#{id}")
	void updateFood(Food food);

	/**
	 * 删除指定ID的菜品
	 *
	 * @param id 菜品ID
	 */
	@Delete("DELETE FROM foods WHERE id = #{id}")
	void deleteFoodById(@Param("id") Long id);

	@Select("<script>" +
			"SELECT * FROM foods WHERE id IN " +
			"<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
			"#{id}" +
			"</foreach>" +
			"</script>")
	List<Food> selectByIds(@Param("ids") Collection<Long> ids);


	/**
	 * 根据分类ID查询菜品
	 * @param categoryId
	 * @return
	 */
	@Select("SELECT * FROM foods WHERE categoryId = #{categoryId}")
	List<Food> selectFoodsByCategoryId(Long categoryId);

	/**
	 * 保存七牛云的图片URL
	 * @param id
	 * @param imageUrl
	 */
	 @Update("UPDATE foods SET imageUrl = #{imageUrl} WHERE id = #{id}")
	 void saveImageUrl(Long id, String imageUrl);

}